#include "sms.h"

Animate *animate_new (GooCanvasItem * parent, char *image_path,
		                                      gdouble start_x, gdouble start_y,
		                                      gdouble stop_x, gdouble stop_y,
		                                      gdouble init_scale, gdouble init_rotation,
		                                      gdouble animate_scale, gdouble animate_rotation,
		                                      gboolean isAbsolute, gint total_time, gint step_time, GooCanvasAnimateType type)
{
	g_printf("~~~%s\n" ,__FUNCTION__);
	Animate *animate;
	
	if (!parent)
		return NULL;
	
	animate = (Animate *) g_malloc0 (sizeof (Animate) + 1);
	animate->start_x = start_x;
	animate->start_y = start_y;
	animate->stop_x = stop_x;
	animate->stop_y = stop_y;
	
	animate->init_scale = init_scale;
	animate->init_rotation = init_rotation;
	animate->animate_scale = animate_scale;
	animate->animate_rotation = animate_rotation;
	animate->isAbsolute = isAbsolute;
	animate->total_time = total_time;
	animate->step_time = step_time;
	animate->type = type;
	
	animate->parent = parent;
	animate->next = NULL;
	
	animate->animate_pix = gdk_pixbuf_new_from_file (image_path ,NULL);
	animate->cavas = goo_canvas_image_new (parent, animate->animate_pix, 0, 0, NULL);
	goo_canvas_item_set_simple_transform(animate->cavas, animate->start_x, animate->start_y, animate->init_scale, animate->init_rotation);
	goo_canvas_item_animate (animate->cavas, animate->stop_x, animate->stop_y, 
			                                 animate->animate_scale, animate->animate_rotation, 
			                                 animate->isAbsolute, 
			                                 animate->total_time, animate->step_time, animate->type);
	return animate;
}

void animate_destroy (Animate *animate)
{
	if(animate->animate_pix)
		g_object_unref(animate->animate_pix);
	if(animate->cavas)
		g_object_unref(animate->cavas);
	g_free(animate);
}

void animate_stop (Animate *animate)
{
	goo_canvas_item_stop_animation (animate->cavas);
}

void animate_restart (Animate *animate)
{
	goo_canvas_item_set_simple_transform(animate->cavas ,  animate->start_x, animate->start_y, animate->init_scale, animate->init_rotation);
	goo_canvas_item_animate (animate->cavas, animate->stop_x, animate->stop_y, 
				                                 animate->animate_scale, animate->animate_rotation, 
				                                 animate->isAbsolute, 
				                                 animate->total_time, animate->step_time, animate->type);
}

void animate_continue (Animate *animate)
{
	gdouble x, y, scale, rotation;
	if (goo_canvas_item_get_simple_transform (animate->cavas, &x, &y, &scale, &rotation))
	{
		g_print ("x: %f, y: %f, scale: %f, rotation: %f.\n", x, y, scale, rotation);
	}

	printf ("x*total/step: %d\n", abs(x)*animate->total_time/animate->step_time);

	goo_canvas_item_animate(animate->cavas, animate->stop_x, animate->stop_y,
			                                animate->animate_scale, animate->animate_rotation,
			                                animate->isAbsolute,
			                                abs(x)*animate->total_time/animate->step_time, 
			                                animate->step_time, animate->type);
}






